﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace NoteHelper.Model
{
    public enum FlowType
    {
        /// <summary>
        /// 支出
        /// </summary>
        [Display(Name = "支出")]
        FlowOut = -1,
        /// <summary>
        /// 收入
        /// </summary>
        [Display(Name = "收入")]
        FlowIn = 1
    }

    /// <summary>
    /// 账户流水
    /// </summary>
    public class AccountFlow
    {
        public AccountFlow()
        {
            BusinessNo = "";
        }

        [DisplayName("系统编号")]
        public int Id { get; set; }

        [DisplayName("发生日期")]
        public DateTime HappenDate { get; set; }

        [DisplayName("发生账号编号")]
        [StringLength(4)]
        public string AccountId { get; set; }

        [DisplayName("发生账号")]
        public virtual Account MainAccount { get; set; }

        [DisplayName("转入账号编号")]
        [StringLength(4)]
        public string ToAccountId { get; set; }

        [DisplayName("转入账号")]
        public virtual Account ToAccount { get; set; }

        [DisplayName("转账交易码")]
        [StringLength(4)]
        public string BusinessNo { get; set; }

        [DisplayName("备注")]
        [StringLength(256)]
        public string Description { get; set; }

        [MinDecimal()]
        [DisplayName("发生金额")]
        public decimal Amount { get; set; }

        [MinDecimal()]
        [DisplayName("期末余额")]
        public decimal LeftAmount { get; set; }

        [DisplayName("账号流水")]
        public int FlowSeq { get; set; }

        [DisplayName("业务类型")]
        public FlowType BizType { get; set; }

        public AccountFlow CreateToFlow()
        {
            if (string.IsNullOrEmpty(ToAccountId) || BizType == FlowType.FlowIn) return null;

            var entity = new AccountFlow();
            entity.HappenDate = HappenDate;
            entity.AccountId = ToAccountId;
            entity.Amount = Amount;
            entity.BizType = FlowType.FlowIn;
            entity.Description = string.Format("从{0}转入.", AccountId);
            entity.BusinessNo = BusinessNo;
            return entity;
        }
    }
}
